package git.soulbgm.pojo;

import git.soulbgm.cache.enumeration.TimeLevel;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 统计信息
 *
 * @author SoulBGM
 * @date 2024-09-21
 */
public class StatisticsInfo {

    /**
     * 统计表前缀
     */
    private String tablePrefix;

    /**
     * 需要统计的时间等级
     */
    private List<TimeLevel> timeLevels;

    /**
     * 聚合函数
     */
    private List<String> aggregation;

    /**
     * 聚合字段
     */
    private Map<String, String> aggregationFields;

    /**
     * 分组字段
     */
    private List<String> groupBy;

    /**
     * 分组字段和代表全部的值
     */
    private Map<String, String> groupByAndAllValue;

    /**
     * 默认所有值
     */
    private String defaultAllValue = "999999";

    /**
     * 唯一标识符字段
     */
    private String idField = "id";

    /**
     * 统计开始时间字段
     */
    private String statisticsStartTimeField = "statistics_start_time";

    /**
     * 统计结束时间字段
     */
    private String statisticsEndTimeField = "statistics_end_time";


    public String getTablePrefix() {
        return tablePrefix;
    }

    public void setTablePrefix(String tablePrefix) {
        this.tablePrefix = tablePrefix;
    }

    public List<TimeLevel> getTimeLevels() {
        return timeLevels;
    }

    public void setTimeLevels(List<TimeLevel> timeLevels) {
        this.timeLevels = timeLevels;
    }

    public List<String> getAggregation() {
        return aggregation;
    }

    public void setAggregation(List<String> aggregation) {
        this.aggregationFields = aggregation.stream().collect(Collectors.toMap(o -> o.substring(o.indexOf("(") + 1, o.lastIndexOf(")")), Function.identity(),
                (existing, replacement) -> existing, LinkedHashMap::new));
        this.aggregation = aggregation;
    }

    public Map<String, String> getAggregationFields() {
        return aggregationFields;
    }

    public Collection<String> getGroupBy() {
        if (groupBy == null && groupByAndAllValue != null) {
            return groupByAndAllValue.keySet();
        }
        return groupBy;
    }

    public void setGroupBy(List<String> groupBy) {
        this.groupBy = groupBy;
    }

    public Map<String, String> getGroupByAndAllValue() {
        return groupByAndAllValue;
    }

    public void setGroupByAndAllValue(List<String> groupByAndAllValue) {
        this.groupByAndAllValue = groupByAndAllValue.stream().collect(Collectors.toMap(o -> {
                    int endIndex = o.indexOf("(");
                    if (endIndex == -1) {
                        return o;
                    }
                    return o.substring(0, endIndex);
                }, o -> {
                    int index = o.indexOf("(");
                    if (index == -1) {
                        return defaultAllValue;
                    }
                    return o.substring(index + 1, o.lastIndexOf(")"));
                },
                (existing, replacement) -> existing, LinkedHashMap::new));
    }

    public String getDefaultAllValue() {
        return defaultAllValue;
    }

    public void setDefaultAllValue(String defaultAllValue) {
        this.defaultAllValue = defaultAllValue;
    }

    public String getIdField() {
        return idField;
    }

    public void setIdField(String idField) {
        this.idField = idField;
    }

    public String getStatisticsStartTimeField() {
        return statisticsStartTimeField;
    }

    public void setStatisticsStartTimeField(String statisticsStartTimeField) {
        this.statisticsStartTimeField = statisticsStartTimeField;
    }

    public String getStatisticsEndTimeField() {
        return statisticsEndTimeField;
    }

    public void setStatisticsEndTimeField(String statisticsEndTimeField) {
        this.statisticsEndTimeField = statisticsEndTimeField;
    }

}
